<template>
	<view>
		<!-- 返回上一页 -->
		<view class=""
			style="width: 100%;padding-top: 60rpx; margin: auto; height: 100rpx;  display: flex; align-items: center; background: #fff;">
			<image @click="toReturn" style="width: 50rpx; height: 50rpx; margin-left: 40rpx;"
				src="../../../static/images/goBack.png">
			</image>
			<span style="margin-left: 30rpx;">设置</span>
		</view>
		<!-- 展示 -->
		<view class="" style="margin-top: 20rpx;">
			<view style="width: 100%; height: 100rpx; background: #fff; ">
				<view
					style=" padding-top: 20rpx; width: 90%;margin: auto;display: flex; justify-content: space-between;align-items: center;">
					<p>自动购买</p>
					<switch :checked="checked" color="#FFCC33" style="transform:scale(0.7)" @change="switchChange" />
				</view>

			</view>
			<view style="width: 100%; height: 100rpx; background: #fff; margin-top: 20rpx;" @click="showDialog">
				<view
					style=" padding-top: 30rpx; width: 90%;margin: auto;display: flex; justify-content: space-between;align-items: center;">
					<p>清理缓存</p>
				</view>
			</view>
			<view @click="toexit()" style="width: 100%; height: 100rpx; background: #fff; margin-top: 20rpx;">
				<view
					style=" padding-top: 30rpx; width: 90%;margin: auto;display: flex; justify-content: space-between;align-items: center;">
					<p>退出登录</p>
				</view>
			</view>
		</view>
		<!-- 弹窗 -->
		<view>
			<!-- 弹窗部分 -->
			<view class="dialog-mask" v-if="dialogVisible">
				<view class="dialog-content">
					<text style="padding:10rpx 20rpx; font-size: 35rpx; font-weight: 700;">温馨提示</text>
					<p style="padding:20rpx 100rpx ;">您确定要删除缓存文件吗？</p>
					<view
						style="color: #1a73e8; display: flex;padding-top: 40rpx;justify-content: space-between; padding: 20rpx 80rpx;">
						<p @click="okDialog">确定</p>
						<p @click="hideDialog">取消</p>
					</view>

				</view>
			</view>
			<!-- 退出登录 -->
			<view class="">
				<uni-popup ref="popup">
					<view style="width: 600rpx; background: #fff; border-radius: 20rpx;">
						<h3 style="padding: 20rpx;">温馨提示</h3>
						<p style="padding: 20rpx;">您确定要退出登录吗</p>
						<view class="" style="display: flex; justify-content: flex-end; padding: 20rpx;">
							<p @click="toOk" style=" color: #1a73e8; padding-right: 30rpx;">确定</p>
							<p @click="toNo" style=" color: #1a73e8; ">取消</p>
						</view>
					</view>
				</uni-popup>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		postUserLogout,
		getautokou,
		getMyUserInfo
	} from '@/http/api.js'
	export default {
		data() {
			return {
				checked:'',
				dialogVisible: false,
				autokou1: '',
				userInfo: uni.getStorageSync('userInfo'),
			};
		},
		onLoad() {
			this.MyUserInfo()
		},
		methods: {

			switchChange(e) {
				console.log(e.detail.value);
				if (e.detail.value == true) {
					this.autokou1 = 1
					
				} else  if(e.detail.value == false){
					this.autokou1 = 0
					
				}
				this.autokouA()
				this.MyUserInfo()
				console.log(e.detail.value);
				console.log(this.autokou1, "购买");
			},
			// 用户信息
			MyUserInfo(){
				let data = {
					"token": this.userInfo.userinfo.token
				}
				getMyUserInfo(data).then((res)=>{
					this.autokou1 =res[1].data.data.autokou
				if(this.autokou1==1){
					this.checked=true
				}else{
					this.checked=false
				}
					console.log(res[1].data.data.autokou,"goumai");
				})
			},
			autokouA() {
				let data = {
					"token": this.userInfo.userinfo.token,
					"autokou": this.autokou1,
				}

				getautokou(data).then((res) => {
					console.log(res[1].data, "走动购买");
					if(res[1].data.code==1){
						uni.showToast({
							title: '修改成功',
							duration: 2000,
							icon: "none"
						});
					}
				})
			},
			// 退出登录
			UserLogout() {
				let data = {
					"token": this.userInfo.userinfo.token, //用户token
				}
				postUserLogout(data).then((res) => {
					console.log(res[1].data.data, "退出登录");
				})
			},
			// 返回上一页
			toReturn() {
				uni.navigateBack();
			},
			// 打开弹窗
			showDialog() {
				this.dialogVisible = true;
			},
			// 关闭弹窗 取消
			hideDialog() {
				this.dialogVisible = false;
			},
			// 关闭弹窗 确认
			okDialog() {
				  // let storageInfo = uni.getStorageInfoSync();
				  // let size = storageInfo.currentSize;
				  // const token = uni.getStorageSync('userInfo'); // 获取token
				  // uni.clearStorageSync(); // 清除除token以外的所有缓存
				  // uni.setStorageSync('token', token.userinfo.token); // 重新存储token
				  console.log(size, '缓存已清除1');
				uni.showToast({
					title: '缓存已清除',
					icon: 'success'
				});
				this.dialogVisible = false;
			},
			// 退出登录
			toexit() {
				this.$refs.popup.open('center')
			},
			// 确定
			toOk() {
				uni.navigateTo({
					url: '/pages/logon/logon'
				})
				this.$refs.popup.close()
			},
			// 取消
			toNo() {
				this.$refs.popup.close()
			}

		}
	}
</script>

<style lang="scss">
	uni-page-body,
	uni-page-refresh {
		background: #f8f8f8;
		height: 100%;

	}

	.dialog-mask {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.dialog-content {
		background-color: #fff;
		padding: 40rpx 0rpx;

	}
</style>